﻿using LaunchAllaround.DesignPattern.MVC;
using LaunchAllaround.Key;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace LaunchAllaround.Pad
{
    /// <summary>
    /// Logique d'interaction pour PadUI.xaml
    /// </summary>
    public partial class PadUI : UserControl, View<PadModel>, Controller<PadModel>
    {

        private Model<PadModel> model;
        private Grid myGrid;


        public PadUI(PadModel model)
        {
            this.model = model;
            InitializeComponent();
            this.init();
        }

        public PadUI()
        {
            this.model = new PadModel(this);
            InitializeComponent();
            this.init();            
        }

        private void init()
        {
            this.myGrid = new Grid();
            this.myGrid.HorizontalAlignment = HorizontalAlignment.Left;
            this.myGrid.VerticalAlignment = VerticalAlignment.Top;

            
            this.myGrid.Height = 400;
            this.myGrid.Width = 800;

            int colsNumber = ((PadModel)this.GetModel()).Columns;
            int rowsNumber = ((PadModel)this.GetModel()).Rows;
            this.reDraw(rowsNumber, colsNumber);

            this.myGrid.ShowGridLines = true;
            this.myGrid.Background = new SolidColorBrush(((PadModel)this.model).Color);
            this.Content = this.myGrid;
        }

        public void Refresh(RefreshEvent<PadModel> ev)
        {
            //listen to the model
            if (ev is RefreshColor)
            {
                Color color = ((RefreshColor)ev).Color;
                this.myGrid.Background = new SolidColorBrush(color);
            }
            else if (ev is RefreshRowsColumns)
            {
                RefreshRowsColumns e = (RefreshRowsColumns)ev;
                this.reDraw(e.Rows, e.Columns);
            }
        }

        private void reDraw(int rows, int columns)
            {
            // Init part
            this.myGrid.ColumnDefinitions.Clear();
            for (int i = 0; i < columns; i++)
            {
                this.myGrid.ColumnDefinitions.Add(new ColumnDefinition());
            }
            this.myGrid.RowDefinitions.Clear();
            for (int i = 0; i < rows; i++)
            {
                this.myGrid.RowDefinitions.Add(new RowDefinition());
            }

            // adding KeyUIs part
            int KeyIndex = 0;
            PadModel pad = (PadModel)this.GetModel();
            this.myGrid.Children.Clear();
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    KeyUI kui = (KeyUI)pad.Get(KeyIndex).GetViews().Get(0);
                    KeyIndex++;
                    Grid.SetColumn(kui, j);
                    Grid.SetRow(kui, i);
                    this.myGrid.Children.Add(kui);
            }
        }
        }

        public void SetModel(Model<PadModel> m)
        {
            this.model = m;
        }

        public Model<PadModel> GetModel()
        {
            return this.model;
        }
    }
}
